package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;


//菜品模块
@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired  //注入口味持久层
    private DishFlavorMapper dishFlavorMapper;

    @Autowired //注入菜品持久层
    private DishMapper dishMapper;

    @Autowired //注入分类持久层
    private CategoryMapper categoryMapper;

    // 分页+条件查询
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.构建菜品条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name), Dish::getName, name);
        // 2.开启分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        // 3.分页查询
        page = dishMapper.selectPage(page, qw);

        // 4.遍历菜品列表
        List<Dish> dishList = page.getRecords();
        for (Dish dish : dishList) {
            // 根据菜品id查询口味列表
            LambdaQueryWrapper<DishFlavor> dqw = new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getDishId, dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dqw);
            // 封装口味列表
            dish.setFlavors(dishFlavorList);
            // 根据分类id查询分类信息
            Category category = categoryMapper.selectById(dish.getCategoryId());
            // 封装分类名称
            dish.setCategoryName(category.getName());
        }

        // 5.返回分页对象
        return page;
    }

    //新增菜品
    @Override
    public void save(Dish dish) {
        //1.新增菜品基本信息
        log.info("新增之前:" + dish.getId());
        dishMapper.insert(dish);
        log.info("新增后:" + dish.getId()); //主键返回
        //遍历口味
        for (DishFlavor flavor : dish.getFlavors()) {
            //关联菜品id
            flavor.setDishId(dish.getId());
            //新增口味
            dishFlavorMapper.insert(flavor);
        }
    }

    //回显菜单
    @Override
    public Dish findById(Long id) {
        //1.先查菜品
        Dish dish = dishMapper.selectById(id);
        //2.根据菜品id查询口味
        //2-1构建条件
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, id);
        //2-2 调用mapper
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(qw);
        //3.将口味设置到菜品
        dish.setFlavors(dishFlavorList);
        //4.返回菜品
        return dish;
    }

    //修改菜品
    @Override
    public void updateById(Dish dish) {
        //1.修改菜单基本信息
        dishMapper.updateById(dish);
        //2.根据菜品id修改原来口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(qw);
        //3.遍历新增口味
        for (DishFlavor flavor : dish.getFlavors()) {
            //关联菜品id
            flavor.setDishId(dish.getId());
            //口味新增
            dishFlavorMapper.insert(flavor);
        }
    }

    //批量删除菜品
    @Override
    public void deleteBathIds(List<Long> ids) {
        //查询菜品是否为停售
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        wrapper.eq(Dish::getStatus, 1);
        Integer selectCount = dishMapper.selectCount(wrapper);
        if (selectCount > 0) {
            throw new CustomException("删除的菜品状态必须为停售");
        }
        //先删菜品
        dishMapper.deleteBatchIds(ids);
        //再删口味 构建条件
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.in(DishFlavor::getDishId, ids);
        dishFlavorMapper.delete(qw);


    }

    //停售起售
    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //条件对象
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.in(Dish::getId, ids);
        //修改实体
        Dish dish = new Dish();
        dish.setStatus(status);
        //调用mapper
        dishMapper.update(dish, qw);

    }

    //根据分类查询菜品列表
    @Override
    public List<Dish> findListByCategoryId(Long categoryId,String name) {
        //构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        //根据分类查询
        qw.eq(categoryId!=null,Dish::getCategoryId, categoryId);
        //判断起售状态
        qw.eq(Dish::getStatus, 1);
        qw.like(StrUtil.isNotBlank(name),Dish::getName,name);
        //2.调用mapper查询并返回
        return dishMapper.selectList(qw);
    }

    //菜品展示
    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
        //1.查询该分类下启售的菜品有哪些
        //1-1构建条件
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId, categoryId);//所属分类都有哪些菜品
        qw.eq(Dish::getStatus, status); //哪些为启售
        //1-2mapper查询
        List<Dish> dishList = dishMapper.selectList(qw);
        //遍历菜品
        for (Dish dish : dishList) {
            //根据菜品id查询口味
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dish.getId()));
            //添加到菜品中
            dish.setFlavors(dishFlavors);

        }
        return dishList;
    }


}
